#!/bin/bash

# 错误时退出
set -e

# 清理函数
clean() {
    echo "清理构建产物..."
    rm -f calculator.o libcalculator.dylib calculator
}

# 编译 C++ 代码
build_cpp() {
    echo "编译 calculator.cpp..."
    g++ -fPIC -Wall -std=c++11 -c calculator.cpp -o calculator.o
    
    echo "生成动态库..."
    g++ -shared -o libcalculator.dylib calculator.o
}

# 构建 Go 程序
build_go() {
    echo "构建 Go 程序..."
    go build -o calculator main.go
}

# 根据参数执行不同的操作
case "$1" in
    "clean")
        clean
        ;;
    "cpp")
        build_cpp
        ;;
    "go")
        build_go
        ;;
    *)
        # 默认构建所有
        build_cpp
        build_go
        ;;
esac